from exts import db, bcrypt
from flask_login import AnonymousUserMixin, UserMixin
import datetime
import shortuuid


class User(UserMixin, db.Model):
    __tablename__ = 'user'
    id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid)
    email = db.Column(db.String(50), nullable=False, unique=True)
    username = db.Column(db.String(100), nullable=False)
    _password = db.Column(db.String(150), nullable=False)
    enabled = db.Column(db.Integer, default=0)  #是否激活，1激活，0未激活
    join_time = db.Column(db.DateTime, default=datetime.datetime.now)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, password):
        self._password = bcrypt.generate_password_hash(password)

    def check_password(self, old):
        return bcrypt.check_password_hash(self._password, old)


class Car(db.Model):
    """
    汽车表
    """
    __tablename__ = 'car'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    car_code = db.Column(db.String(30), nullable=False, unique=True) #车牌
    car_type = db.Column(db.Integer, nullable=False) #车子类型：0-燃油车，1-电车


class CarRecord(db.Model):
    """
    汽车的每日记录表
    """
    __tablename__ = 'car_record'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    car_code = db.Column(db.String(30), nullable=False) #车牌
    write_time = db.Column(db.Time, nullable=False) #记录时间
    record_day = db.Column(db.Date, default=datetime.datetime.today()) #记录的日期
    kilom = db.Column(db.Integer, nullable=False) #汽车公里数
    than_yest = db.Column(db.Integer,default=0) # 公里数比上日多X
    oil_cost = db.Column(db.Integer, default=0) #油费

    car_id = db.Column(db.Integer, db.ForeignKey('car.id')) # 汽车id，即记录信息的所属车辆id

    car = db.relationship('Car', backref='records')


class elec_Car(db.Model):
    """
    电度表
    """
    __tablename__ = 'elec_car'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    record_day = db.Column(db.Date, nullable=False, default=datetime.datetime.today())
    day_type = db.Column(db.Integer, nullable=False) # 0是白班，1是夜班
    sum_elec = db.Column(db.Integer) #总电
    xiaomaibu = db.Column(db.Integer) #小卖部
    dianzhuang = db.Column(db.Integer) #充电桩
    record_man = db.Column(db.String(50)) #记录人


class Invoice(db.Model):
    """
    发票记录表
    """
    __tablename__ = 'invoice'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    record_time = db.Column(db.Time, nullable=False) #记录时间
    invoice_code = db.Column(db.String(20), nullable=False)
    coun = db.Column(db.Integer, nullable=False)
    record_man = db.Column(db.String(50), nullable=False)
    record_day = db.Column(db.Date, nullable=False, default=datetime.datetime.today())



"""
[
    {
        'day':'2019-01-21',
        'data':[
            数据库查询的对象1,
            数据库查询的对象2,
        ]
    },
    {
        'day’:'2019-01-22',
        'data':[
            {},
            {}
        ]
    }
]
"""